package com.atguigu.minio;

import io.minio.*;

public class Demo01 {
    public static void main(String[] args) throws Exception{
        //1.创建MinioClient对象
        MinioClient minioClient = MinioClient.builder()
                .endpoint("http://192.168.174.100:9000")
                .credentials("minioadmin","minioadmin")
                .build();

        //2.如果桶不存在，则创建桶并设置访问策略
        boolean flag = minioClient.bucketExists(BucketExistsArgs.builder().bucket("b001").build());
        if(!flag){
            //如果桶不存，则创建桶
            minioClient.makeBucket(MakeBucketArgs.builder()
                    .bucket("b001")
                    .build());
            //设置策略
            minioClient.setBucketPolicy(SetBucketPolicyArgs.builder()
                    .bucket("b001")
                    .config("""
                            {
                                "Version": "2012-10-17",
                                "Statement": [
                                    {
                                        "Effect": "Allow",
                                        "Principal": {
                                            "AWS": [
                                                "*"
                                            ]
                                        },
                                        "Action": [
                                            "s3:GetObject"
                                        ],
                                        "Resource": [
                                            "arn:aws:s3:::b001/*"
                                        ]
                                    }
                                ]
                            }
                            """).build());
        }

        //3.调用minioClient的uploadObject()
        minioClient.uploadObject(
                UploadObjectArgs.builder()
                        .bucket("b001")
                        .object("hello.jpg")
                        .filename("D:\\images\\房间-厨房-1.jpg")
                        .build()
        );

    }
}
